/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoRegion;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoRegionService;

import java.util.List;

/**
 * 地区管理Controller
 *
 * @author yt
 * @version 2017-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoRegion")
public class CampusinfoRegionController extends BaseController {

    @Autowired
    private CampusinfoRegionService campusinfoRegionService;

    @ModelAttribute
    public CampusinfoRegion get(@RequestParam(required = false) String id) {
        CampusinfoRegion entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoRegionService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoRegion();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoeducation:campusinfoRegion:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoRegion campusinfoRegion, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoRegion> page = campusinfoRegionService.findPage(new Page<CampusinfoRegion>(request, response), campusinfoRegion);
        List<CampusinfoRegion> campusinfoRegions = campusinfoRegionService.findList(campusinfoRegion);
        model.addAttribute("campusinfoRegions", campusinfoRegions);
        model.addAttribute("page", page);
        return "modules/campusinfoeducation/campusinfoRegionList";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoRegion:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoRegion campusinfoRegion, Model model) {
        model.addAttribute("campusinfoRegion", campusinfoRegion);
        return "modules/campusinfoeducation/campusinfoRegionForm";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoRegion:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoRegion campusinfoRegion, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoRegion)) {
            return form(campusinfoRegion, model);
        }
        CampusinfoRegion findcampusinfoRegion =campusinfoRegionService.getByName(campusinfoRegion);
        if (findcampusinfoRegion==null) {
            campusinfoRegionService.save(campusinfoRegion);
            addMessage(redirectAttributes, "保存地区信息成功");
        } else if(!StringUtils.isBlank(campusinfoRegion.getId())){
            campusinfoRegionService.save(campusinfoRegion);
            addMessage(redirectAttributes, "修改地区信息成功");
        } else {
            addMessage(redirectAttributes, "地区已经存在");
        }
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoRegion/?repage";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoRegion:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoRegion campusinfoRegion, RedirectAttributes redirectAttributes) {
        campusinfoRegionService.delete(campusinfoRegion);
        addMessage(redirectAttributes, "删除地区信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoRegion/?repage";
    }

}